package com.example.emos.wx.config.xss;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

// 使用@WebFilter注解标识该类为过滤器，并指定urlPatterns为"/*"，表示对所有请求进行过滤
@WebFilter(urlPatterns = "/*")
public class XssFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 过滤器初始化方法，通常为空实现
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 将ServletRequest强制转换为HttpServletRequest，以便获取请求对象
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        // 创建XssHttpServletRequestWrapper对象，该对象继承HttpServletRequestWrapper，用于对请求进行XSS过滤
        XssHttpServletRequestWrapper wrapper = new XssHttpServletRequestWrapper(request);

        // 继续执行过滤器链，传递包装后的HttpServletRequest
        filterChain.doFilter(wrapper, servletResponse);
    }

    @Override
    public void destroy() {
        // 过滤器销毁方法，通常为空实现
    }
}
